<template >
   <el-dialog :title="title" :close-on-click-modal="false" :before-close="closeDialog" :visible.sync="visible" width="700px" append-to-body dialogDrag>
        <el-form ref="form" :inline="true" :model="form" class="form" :rules="rules" size="small" label-width="80px">
            <el-form-item label="角色名称" prop="name"> 
                <el-input v-model="form.name" style="width:220px"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio v-for="(value,key) in statusDicts" :key="key" v-model="form.status" :label="value">{{ key }}</el-radio>
            </el-form-item>
            <el-form-item label="排序权值" prop="sort"> 
                <el-input-number v-model="form.sort" controls-position="right" :min="0" :max="999"></el-input-number>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="form.description" style="width: 534px;"></el-input>
            </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
            <el-button type="text"    @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="doSubmit" :loading="loading">确认</el-button>
        </div>
    </el-dialog>
</template>
<script>
import { addRole,editRole,selRole} from '@/api/role'
export default {
    inject:['reload'],
    data() {
        return {
            rowId:'',
            title:'',
            visible: false,
            statusDicts:this.$global.status,
            loading:false,  //确认提交按钮加载中
            form: { name:'', description:'', sort: 0, status: 1},
            rules:{
                name: [{ required: true,message: '用户名不能为空'}],
            }
        };
    },
    methods: {
        closeDialog(){
            this.$refs['form'].resetFields();
            this.visible = false
            this.loading = false
            this.rowId = ''
            this.form = { name:'', description:'', sort: 0, status: 1}
        },
        init(row,isAdd) {
            this.title = (isAdd ? '新增用户':'编辑用户')
            !!row && (this.rowId = row.id) && this.findRoleById(this.rowId)
            this.$nextTick(() => {
                this.visible = true
            })
        },
        findRoleById(id){
            this.apiSelRoleById(id)
        },
        doSubmit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loading = true
                    this.$confirm("确定?",'提示',{ type: 'warning'}).then( ()=>{
                        if(this.rowId){
                            this.apiEditRole(this.form)
                        }else{
                            this.apiAddRole(this.form)
                        }
                    }).catch(err=>{
                        this.loading =false
                    })
                }
            })
        },
        //==========>请求api接口<==========//
        apiSelRoleById(id){
            selRole(id).then(res=>{
                if(res.code === 200){
                    this.form ={...res.data}
                }
            },error=>{
                this.$message.error(res.message)
            })
        },
        apiEditRole(admin){
            editRole(admin).then(res => {
                if(res.code === 200){
                    this.$message.success(res.message)
                    this.closeDialog()
                    this.reload()
                }
            },error =>{
                this.loading = false
            })
        },
        apiAddRole(admin){
            addRole(admin).then(res => {
                if(res.code === 200){
                    this.$message.success(res.message)
                    this.closeDialog()
                    this.reload()
                }
            },error =>{
                this.loading = false
            })
        }
        //==========>请求api接口<==========//
    },
};
</script>
<style scoped>

</style>


